home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / sync.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  2KB  |  131 lines

  1. /*
  2.  * FILE
  3.  *    sync.c
  4.  *    
  5.  *    
  6.  * DESCRIPTION
  7.  *    syncing filesystems, makes only sense with mint and
  8.  *    minixfs for now
  9.  *    
  10.  * BUGS
  11.  *    minixfs V 060 PL 5 always syncs all drives, so there will
  12.  *    be too much syncing, since we call Dcntl for all known drives.
  13.  *    
  14.  */
  15.  
  16. #include <mintbind.h>
  17. #include <stat.h>
  18. #include <errno.h>
  19. #include <support.h>
  20. #include <string.h>
  21.  
  22. extern int __mint;
  23.  
  24. /* from minixfs.h by S N Henson*/
  25. #define MFS_BASE    0x100
  26. #define MFS_VERIFY    (MFS_BASE)        /* Return minixfs magic number */
  27. #define MFS_SYNC    (MFS_BASE|0x01)    /* Sync the filesystem */
  28. #define MFS_MAGIC    0x18970431        /* Magic number from MFS_VERIFY */
  29.  
  30. /*
  31.  * FUNCTION
  32.  *    int sync(void)
  33.  *    
  34.  * DESCRIPTION
  35.  *    query all known drives for a valid MinixFs
  36.  *    if we find one, sync it.
  37.  */
  38. int
  39. sync()
  40. {
  41.     long            magic;
  42.     unsigned long    drives;
  43.     int                i, rv;
  44.     char path[4];
  45.  
  46.     if (!__mint)
  47.         return 0;
  48.  
  49.     strcpy(path, "A:\\");
  50.     
  51.     drives = Dsetdrv(Dgetdrv());
  52.  
  53.     drives &= ~0x3;    /* don't sync the floppys */
  54.     
  55.     for (i = 2; drives ; i++) {
  56.         if (drives & (1L << i)) {
  57.             drives &= ~(1L << i);
  58.             path[0] = 'A' + i;
  59.             magic = 0L;
  60.             if (!Dcntl(MFS_VERIFY, path, &magic) && magic == MFS_MAGIC) {
  61.                 if ((rv = (int)Dcntl(MFS_SYNC, path, 0L)) < 0) {
  62.                     errno = -rv;
  63.                     return -1;
  64.                 }
  65.             }
  66.         }
  67.     }
  68.         
  69.     return 0;
  70. }  /* sync() */
  71.  
  72.  
  73.  
  74. /*
  75.  * FUNCTION
  76.  *    int fsync(int fd)
  77.  *    
  78.  * DESCRIPTION
  79.  *    sync all buffers related to file descriptor fd
  80.  *    since MFS 605 always syncs all the buffers, we don't bother
  81.  *    to get the full path.
  82.  */
  83. int
  84. fsync(fd)
  85.     int    fd;
  86. {
  87.     int            rv;
  88.     long        magic     = 0L;
  89.     char        path[4];
  90.     struct stat    statbuf;
  91.  
  92.     if (!__mint)
  93.         return 0;
  94.     
  95.     strcpy(path, "A:\\");
  96.     if (fstat(fd, &statbuf))
  97.         return -1;            /* errno set from fstat */
  98.  
  99.     if (statbuf.st_dev >= 32)
  100.       /* If mounted via FS_MOUNT, st_dev will be > 0x100.
  101.          Pretend that it worked. */
  102.       return 0;
  103.  
  104.     path[0] = 'A'+ statbuf.st_dev;
  105.     if (!Dcntl(MFS_VERIFY, path, &magic) && magic == MFS_MAGIC) {
  106.         if ((rv = (int)Dcntl(MFS_SYNC, path, 0L)) < 0) {
  107.             errno = -rv;
  108.             return -1;
  109.         }
  110.     }
  111.  
  112.     return 0;
  113. }  /* fsync() */
  114.  
  115.  
  116. #ifdef TEST
  117.  
  118. /*
  119.  * Im not in the mood to write a tricky test routine,
  120.  * so just do 'cat junk1 >junk2;sync' from your shell
  121.  * and listen to your harddisk.
  122.  */
  123. int
  124. main()
  125. {
  126.     sync();
  127.     return 0;
  128. }
  129.  
  130. #endif
  131.